import org.apache.commons.logging.impl.Log4JLogger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class JdbcUtils {

    private static String driverClass ;
    private static String url ;
    private static String user ;
    private static String password ;

    static{

        driverClass = "com.mysql.jdbc.Driver";
        url = "jdbc:mysql://localhost:3306/reindex?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        user = "root";
        password ="123456";

        /**
         * 驱动注册
         */
        try {
            Class.forName(driverClass);//注册加载驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 Connetion
     * @return
     * @throws SQLException
     */

    public static Connection getConnection() {
        Connection conn = null;
        try {
             conn = DriverManager.getConnection(url, user, password);

        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭ResultSet资源
     * @param rs
     */

    public static void close(ResultSet rs){
        if(null != rs){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭Statement资源
     * @param stmt
     */

    public static void close(Statement stmt){
        if(null != stmt){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭close资源
     * @param conn
     */
    public static void close(Connection conn){
        if(null != conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 关闭资源
     * @param rs
     * @param stmt
     * @param conn
     */

    public static void close(ResultSet rs,Statement stmt,Connection conn){
        close(rs);
        close(stmt);
        close(conn);
    }
    /**
     * insert方法
     * @param conn
     * @param sql
     * @param parems
     * @return
     * @throws SQLException
     */
    public static int insert(Connection conn,String sql,Object[] parems)  {
        PreparedStatement stmt = null;
        try{
            stmt = conn.prepareStatement(sql);
            if(null != parems && parems.length>0){//判断是否有信息增加
                for(int i = 0;i<parems.length;i++){
                    stmt.setObject(i+1, parems[i]);//使用给定对象设置指定参数的值
                }
            }
            int count = stmt.executeUpdate();

            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }finally{
            close(stmt);
        }
    }

//    public static void main(String[] args) {
//        String sql="INSERT INTO `record`(`url_id`, `url`) VALUES ('111', '555') ";
//        insert(getConnection(),sql,null);
//    }

}
